package jyj.springmvc.learning.controller;

/**
 * @Author 老顽固小可爱
 * @Date 2022/12/12 13:19
 * @Version 1.0
 */

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author c语言中文网
 */
@Controller
public class ForwardController {
    /**
     * 通过控制器方法返回值实现转发
     *
     * @param model
     * @return
     */
    @RequestMapping("/testForward")
    public String testDispatcher(Model model) {
        //通过 Model 向 request 域中添加信息
        model.addAttribute("requestScope", "控制器返回值：转发！");
        //通过返回值将请求转发的 /userPage 上
        return "forward:/user";
    }

    /**
     * 通过 ModeAndView 实现转发
     *
     * @return
     */
    @RequestMapping("/testForward2")
    public ModelAndView testForward2() {
        ModelAndView modelAndView = new ModelAndView();
        //设置逻辑视图名
        modelAndView.setViewName("forward:/user");
        //在request域对象中添加信息
        modelAndView.addObject("requestScope", "ModelAndView：转发！");
        return modelAndView;
    }
}
